package com.bangbang.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


//@Configuration
public class ShiroConfig {

//    创建shiro过滤器链
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Autowired DefaultWebSecurityManager securityManager){
//        创建过滤器工厂bean  ==  实体类  对象
        return null;
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Autowired LoginRealm loginRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        关联roalm  我们自己写的登录和授权逻辑
        securityManager.setRealm(loginRealm);
        return securityManager;
    }

    @Bean
    public LoginRealm loginRealm(@Autowired HashedCredentialsMatcher matcher){
        LoginRealm loginRealm = new LoginRealm();
//        开启密码加密功能
        loginRealm.setCredentialsMatcher(matcher);
        return loginRealm;
    }

    @Bean
//    开启密文加密
    public HashedCredentialsMatcher credentialsMatcher(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//        指定加密方式
        matcher.setHashAlgorithmName("MD5");
//        加密次数
        matcher.setHashIterations(4);
//        设置一下编码
        matcher.setStoredCredentialsHexEncoded(true);
        return matcher;
    }

}
